clear all
cd "C:\Users\Public\Documents\ImmPanelRevis19\"
cd "ImmPanelRevis19\Donnees\select"

/* correspondance table reg - dep */
use final.dta, clear
drop _freq
gen reg=substr(ze,1,2)
contract dep reg
drop if reg=="00"
rename dep depr
rename reg regr
drop _freq
save tp1.dta, replace


use final.dta, clear
drop _freq
gen reg=substr(ze,1,2)
contract dep reg
drop if reg=="00"
rename dep adepr
rename reg aregr
drop _freq
save tp2.dta, replace

/***************/
/* Estimations */
/***************/
capture program drop regsamp
program define regsamp
version 11
syntax, occp(name) outc(name) file(name) 

use final.dta, clear

gen reg=substr(ze,1,2)
gen nreg="11" if reg=="11" | reg=="00"
replace nreg="24" if reg=="24"
replace nreg="27" if reg=="26" | reg=="43"
replace nreg="28" if reg=="23" | reg=="25"
replace nreg="32" if reg=="31" | reg=="22"
replace nreg="44" if reg=="42" | reg=="21" | reg=="41"
replace nreg="52" if reg=="52" 
replace nreg="53" if reg=="53" 
replace nreg="75" if reg=="72" | reg=="74" | reg=="54"
replace nreg="76" if reg=="91" | reg=="73"
replace nreg="84" if reg=="83" | reg=="82"
replace nreg="93" if reg=="93" 
replace nreg="94" if reg=="94" 

xi , pre(REG_) i.nreg

gen occ="bc" if bc==1
replace occ="cad" if cad==1

gen areg=substr(aze,1,2)

/* region of residence */
joinby depr using tp1.dta, unm(m)
tab _merge 
drop _merge

joinby adepr using tp2.dta, unm(m)
tab _merge 
drop _merge

/* change commuting zones AND region of residency */
gen lshiftr=(ze!=aze & regr!=aregr)

/* change commuting zones AND SAME region of residency */
gen lshiftsr=(ze!=aze & regr==aregr)

/* change commuting zones AND departement */
gen lshiftd=(ze!=aze & (depr!=adepr | regr!=aregr))

/* change commuting zones AND SAME departement */
gen lshiftsd=(ze!=aze & depr==adepr)

/* create some outcome variables */
gen ldp=ln(dp/l.dp)
gen dftfy=(dp==360)-(L.dp==360)
gen oshift=(`occp'==0 & L.`occp'==1)
replace oshift=( (cad==0 & L.cad==1) | (tecemp2==0 & L.tecemp2==1) | (bc==0 & L.bc==1) | (bcnq==0 & L.bcnq==1)) if "`occp'"=="all"

/* keep individuals in the occupation group */
keep if (btime==1 & `occp') | (btime==2 & L.`occp')

/* location in the decile of the initial group distribution */
gen lsnd=ln(sn/dp)
/* residual wages for each year */
quietly: reg lsnd F_* 
predict rlsnd, residuals
/* change in residual wages */
gen drlsnd=D.rlsnd
/* residual outcome */
quietly: reg `outc' F_*  if btime==2
predict r`outc' if btime==2, residuals
quietly: reg `outc'r F_*  if btime==2
predict r`outc'r if btime==2, residuals
quietly: reg `outc'sr F_*  if btime==2
predict r`outc'sr if btime==2, residuals
quietly: reg `outc'd F_*  if btime==2
predict r`outc'd if btime==2, residuals
quietly: reg `outc'sd F_*  if btime==2
predict r`outc'sd if btime==2, residuals

/* rank in the wage distribution */
/*drop F_* */
egen qinit = fastxtile(rlsnd) if ftreat, by(ze an btime) nq(100)
replace qinit=qinit/100

/* interaction term and instruments */
gen lqinit=L.qinit
gen inter= dimm*L.qinit
gen linter=ldimm*L.qinit
gen inter75= dimm75 * L.qinit
gen linter75=ldimm75*L.qinit
gen interm2= dimtm2 * L.qinit
gen linterm2=ldimtm2 *L.qinit

keep if btime==2 & L.`occp' & !missing(dimm) & !missing(dimm75)
foreach year in 1982 1991 1999 2007 {
/* winsorize drlsnd */
quietly: sum drlsnd if an==`year', d
replace drlsnd=. if drlsnd>`r(p99)' & an==`year'
replace drlsnd=. if drlsnd<`r(p1)' & an==`year'
}
drop if missing(drlsnd)
drop if missing(basman)
drop if missing(bascom)

/* weight inverse of the size of the group in the CZ */
save temp.dta, replace
gen dummy=1
collapse (sum) nbze = dummy , by(ze an)
gen iw2=1/ nbze

save iw2.dta, replace
use temp.dta, replace
capture drop _merge
joinby ze an using iw2.dta , unm(m)
drop _merge

/*
gen yberni=rbinomial(1,0.004)
keep if yberni==1
*/
collapse rlshift rlshiftr rlshiftsr rlshiftd rlshiftsd dimm dimm75 Y_* REG_* , by(ze an)

/* all moves */
ivreg2 rlshift (dimm  = dimm75 )  Y_*  REG_* , cl(ze) partial(Y_*  REG_* )
estimates store `occp'iva

/* other departement */
ivreg2 rlshiftd (dimm  = dimm75 )   Y_*  REG_* , cl(ze) partial(Y_*  REG_* )
estimates store `occp'ivd


end

capture program drop mobtyp
program define mobtyp
version 11
syntax, outc(name) file(name) 


/* OUTFLOWS */
regsamp, occp(all) outc(`outc') file(`file')
regsamp, occp(cad) outc(`outc') file(`file')
regsamp, occp(tecemp2) outc(`outc') file(`file')
regsamp, occp(bc) outc(`outc') file(`file')
regsamp, occp(bcnq) outc(`outc') file(`file')


end
mobtyp , outc(lshift) file(TableA14_LongDist)

 rm "C:\Users\Public\Documents\Tab21\TableA14_LongDist.rtf"

/* IVSELECT */
esttab alliva cadiva tecemp2iva bciva bcnqiva ///
using "C:\Users\Public\Documents\Tab21\TableA14_LongDist.rtf" /// 
, append title("s")  b(%9.3f) cells(b(star fmt(3)) se(fmt(3) par) ) ///
 stats(N widstat) star(* 0.10 ** 0.05 *** 0.01)

 esttab allivd cadivd tecemp2ivd bcivd bcnqivd ///
using "C:\Users\Public\Documents\Tab21\TableA14_LongDist.rtf" /// 
, append title("s")  b(%9.3f) cells(b(star fmt(3)) se(fmt(3) par) ) ///
 stats(N widstat) star(* 0.10 ** 0.05 *** 0.01)
